home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1238 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  146 lines

  1. Path: news.infi.net!usenet
  2. From: nngis@norfolk.infi.net (Greg DiGiorgio)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: File Search Algorithm
  5. Date: 12 Jan 1996 15:40:37 GMT
  6. Organization: Customer of InfiNet
  7. Message-ID: <4d5vdl$5g6@news.infi.net>
  8. References: <1996Jan11.044752.6290@leo.vsla.edu>
  9. Reply-To: nngis@norfolk.infi.net
  10. NNTP-Posting-Host: h-brahe.norfolk.infi.net
  11. Mime-Version: 1.0
  12. X-Newsreader: WinVN 0.99.3
  13.  
  14. In article <1996Jan11.044752.6290@leo.vsla.edu>, tbrown@leo.vsla.edu 
  15. says...
  16. >
  17. >Does anyone have an algorithm for a file search?  One that
  18. >finds a file across multiple directories?  OS-Generic code
  19. >would be welcome, but MS-DOS specific code would be better.
  20. >
  21. >                                Sincerely,
  22. >                                  Todd Brown
  23. >-- 
  24. >Internet: tbrown@pobox.com
  25. >Snail mail: Todd Brown
  26. >            Star Route, Box 452
  27. >            Lottsburg, VA 22511
  28.  
  29. Todd, here's a solution. It uses the "brute-force" method of string
  30. searching and shows how you could code your own "strstr" function.
  31.  
  32. /************************************************************************
  33. ****
  34.                 SEARCH.C
  35. *************************************************************************
  36. ****
  37.     This program acts as a file search utility by searching a file
  38.     line-by-line for a specified string of characters.
  39.  
  40.     Ex:    To search a file called JUNK.C for a string 'while', you
  41.         would enter:
  42.  
  43.             SEARCH JUNK.C while
  44. *************************************************************************
  45. ****/
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <stdlib.h>
  49.  
  50. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= GLOBAL VARIABLES 
  51. =-=-=-=-=-=-=-=-=-=-=-=*/
  52.  
  53. FILE    *infile;
  54. char    filename[80],    /* input file name */
  55.     file_str[133],    /* holds line read from the file */
  56.     sub_str[80];    /* holds the substring to search for in file_str 
  57. */
  58.  
  59.  
  60. /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= FUNCTIONS 
  61. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  62.     getline    - This function automatically opens a file, reads a line
  63.           from it, returns that line to the calling function, and
  64.           closes the file, exitting from the program when it hits
  65.           EOF.
  66.  
  67.           It accepts one input, the variable that will hold the 
  68. line
  69.           read in.  It loads the string variable, "inpline" with 
  70. the 
  71.           line read.
  72. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  73. -=*/
  74.  
  75. void    getline(char inpline[])
  76. {
  77.     static    file_open = 0;
  78.     if (!file_open) {
  79.         infile = fopen(filename,"r");
  80.         if (infile == NULL) {
  81.             printf("Can not open file %s\n",filename);
  82.             exit(99);
  83.         }
  84.     }
  85.     file_open = 1;
  86.     if (fgets(inpline, 132, infile) == NULL) {
  87.         printf("\nEOF reached; program terminating...\n");
  88.         exit(0);
  89.     }
  90. }
  91.  
  92. int find ( char file_str[],  char sub_str[] )
  93. {
  94.     
  95. /*********************************************************************
  96.     This function accepts 2 strings a input:
  97.  
  98.         file_str - this is the line that you read from the file.
  99.         sub_str  - this is the string that you are searching for.
  100.  
  101.     Implementing a BRUTE FORCE string search method, this function 
  102. returns
  103.     a one if 'sub_str' can be found in 'file_str'; else zero.  Also, 
  104. this
  105.     function only checks for the first occurence of 'sub_str' in 
  106. 'file_str'.
  107.     
  108. *********************************************************************/
  109.  
  110.     /*********************** Your Code Goes Here 
  111. ***********************/
  112.     int found;
  113.     int current,substr,searchstr;
  114.  
  115.     found=0; current=0;
  116.     while (current<strlen(file_str)) {
  117.         if (sub_str[0]==file_str[current]) {
  118.             if ( (strlen(file_str)-current)<strlen(sub_str))
  119.                 break;
  120.             substr=0;  searchstr=current;
  121.             while (substr < strlen(sub_str) &&
  122.                    sub_str[substr]==file_str[searchstr] ){
  123.                 substr++; searchstr++;
  124.             }
  125.             if (substr == strlen(sub_str)) {
  126.                 found=1;
  127.                 break;
  128.             }
  129.         }
  130.         current++;
  131.     }
  132.     return(found);
  133. }
  134.  
  135. main(int argc, char *argv[])
  136. {
  137.     strcpy(filename,argv[1]);    strcpy(sub_str, argv[2]);
  138.     do {
  139.         getline(file_str);    /* read a line from the file */
  140.         if (find(file_str,sub_str))
  141.             printf("%s",file_str);
  142.     }
  143.     while (1);
  144. }
  145.  
  146.